; Cache.TXT Copyright (C) 1989 Level 9 Computing.
;
; for Floor Map Loader/Cache.
;
; N.W.Austin 13/7/89
;
;-----

;CACHE.TXT: handle simple floormap cache
;Two entry points:
;   FCInitFloorCache
;   FCloadFloor

;-----

; draw flags are...
; dRemoveRedraw=65535  -1 Remove and redraw
; dInsert=0            insert
; dPlot=1              plot as sprite
; dInsertRedraw=2      insert and redraw
; dMarkPreload=3       mark to preload
; dSetProtect=4        set protection mark
; dUnsetProtect=5      unset protection mark

const
 StartCache1=200
 StartCache2=5220 ;200+5020
 EndCache2=10240  ;200+5020*2

var
 Cache1     ;map number in first cache
 Cache2     ;map number in second cache
 EndTable EndCache1

begin

.FCInitFloorCache ;v1=cache size

 CurrentCache=StartCache1
;* &FloorMap(CurrentCache)=c0 ;Clear cache1
 Cache1=0
 Cache2=0

 v2=0 ;table
 MapNumber=48 ;'0'

.IFC1
;* &FloorMap(CurrentCache)=c0
 push v1
 push v2
 gosub @LoadMap
 pop v2
 pop v1

 v3=CurrentCache
.IFC2
 &v4=FloorMap(v3) ;room number
 if v4=0 then FCnextMap

 add v2,c4
 if v2>v1 then FCtableFull
 sub v2,c4

 &FloorMap(v2)=v4        ;room number
 add v2,c2
 &FloorMap(v2)=MapNumber ;file
 add v2,c2

 add v3,c2
 &v4=FloorMap(v3) ;length-2
 add v3,v4
 goto IFC2

.FCnextMap
 if MapNumber=57 then IFC3 ;'9'
 if MapNumber=90 then IFC4 ;'Z'
 add MapNumber,c1
 goto IFC1
.IFC3
 MapNumber=65 ;'A'
 goto IFC1

.IFC4
 add v2,c4
 if v2<v1 then IFC5

.FCtableFull
 if v2>3 then IFC5
 v2=4
.IFC5
 sub v2,c4
 &FloorMap(v2)=c0
 add v2,c2
 &FloorMap(v2)=c0

 CurrentCache=StartCache2
 &FloorMap(CurrentCache)=c0 ;Clear cache2
 CurrentCache=StartCache1
 &FloorMap(CurrentCache)=c0 ;Clear and select cache1
 MapNumber=0
 return

;-----

.FCloadFloor ;RoomNum=room

 if RoomNum=0 then FCnoRoom

 v1=0 ;cache table
.FLF1
 &v2=FloorMap(v1)
 if v2=0 then FCnoRoom
 if v2=RoomNum then FLF2
 add v1,c4
 goto FLF1

.FLF2
 add v1,c2
 &MapNumber=FloorMap(v1)

 if cache1=MapNumber then FCinCache1
 if cache2=MapNumber then FCinCache2

 if CurrentCache=StartCache1 then FCswitchCache
 CurrentCache=StartCache1
 Cache1=MapNumber
 goto FLF3
.FCswitchCache
 CurrentCache=StartCache2
 Cache2=MapNumber
.FLF3

 gosub @LoadMap

 gosub @FindRoom
 return

.FCinCache1
 CurrentCache=StartCache1
 gosub @FindRoom
 return

.FCinCache2
 CurrentCache=StartCache2

.FCnoRoom ;Set RoomNum=0

 gosub @FindRoom
 return

;-----
